﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Fujitsu.JiraAnalysis.TrackingSystemAdapter;
using Fujitsu.JiraAnalysis.DAL;

namespace Fujitsu.JiraAnalysis.UI
{
    public partial class Map : System.Web.UI.Page
    {
        private TrackSystemInfoAdapter Adapter { get; set; }

        private ResolutionStorage resolutionStorage = new ResolutionStorage();
        private StatusStorage statusStorage = new StatusStorage();
        private PriorityStorage priorityStorage = new PriorityStorage();
        private IssuesTypeStorage issueTypeStorage = new IssuesTypeStorage();

        private int CurrentProjectId
        {
            get
            {
                return int.Parse(dropDownProjects.SelectedValue);
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            Adapter = Session["Adapter"] as TrackSystemInfoAdapter;
            if (Adapter == null)
            {
                Server.Transfer("Login.aspx");
            }
        }

        protected void dropDownProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            fieldsetIssueTypeMapping.Visible = true;
            repeaterIssueTypes.DataSource = Adapter.GetIssueTypesForProjectKey(dropDownProjects.SelectedItem.ToString());
            repeaterIssueTypes.DataBind();

            fieldsetIssuePriorityMapping.Visible = true;
            repeaterIssuePriority.DataSource = Adapter.GetPriorities();
            repeaterIssuePriority.DataBind();

            fieldsetIssueStatusMapping.Visible = true;
            repeaterIssueStatus.DataSource = Adapter.GetStatuses();
            repeaterIssueStatus.DataBind();

            fieldsetIssueResolutionMapping.Visible = true;
            repeaterIssueResolution.DataSource = Adapter.GetResolutions();
            repeaterIssueResolution.DataBind();

            savedHeader.Visible = false;
            btnSave.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveMappingFor("repeaterIssueTypes");
            SaveMappingFor("repeaterIssuePriority");
            SaveMappingFor("repeaterIssueStatus");
            SaveMappingFor("repeaterIssueResolution");

            savedHeader.Visible = true;
        }

        private void SaveMappingFor(string repeaterName)
        {
            foreach (var control in container.FindControl(repeaterName).Controls.OfType<RepeaterItem>())
            {
                var jiraId = control.FindControl("jiraID") as HiddenField;
                var dropDownList = control.FindControl("dropDownList") as DropDownList;

                if (jiraId == null || dropDownList == null) 
                    continue;
                
                var jiraName = control.FindControl("jiraName") as Literal;
                switch (repeaterName)
                {
                    case "repeaterIssueTypes":
                        {
                            issueTypeStorage.UpdateMapping(CurrentProjectId, int.Parse(jiraId.Value), int.Parse(dropDownList.SelectedValue), jiraName.Text);
                            break;
                        }
                    case "repeaterIssuePriority":
                        {
                            priorityStorage.UpdateMapping(CurrentProjectId, int.Parse(jiraId.Value), int.Parse(dropDownList.SelectedValue), jiraName.Text);
                            break;
                        }
                    case "repeaterIssueStatus":
                        {
                            statusStorage.UpdateMapping(CurrentProjectId, int.Parse(jiraId.Value), int.Parse(dropDownList.SelectedValue), jiraName.Text);
                            break;
                        }
                    case "repeaterIssueResolution":
                        {
                            resolutionStorage.UpdateMapping(CurrentProjectId, int.Parse(jiraId.Value), int.Parse(dropDownList.SelectedValue), jiraName.Text);
                            break;
                        }
                    default:
                        break;
                }
            }
        }

        protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var jiraId = e.Item.FindControl("jiraID") as HiddenField;
            var dropDownList = e.Item.FindControl("dropDownList") as DropDownList;

            if (jiraId == null || dropDownList == null)
                return;
            
            var dbId = -1;
            var repeater = sender as Repeater;
            switch (repeater.ID)
            {
                case "repeaterIssueTypes":
                    {
                        dbId = issueTypeStorage.GetIssueTypeMapping(CurrentProjectId, int.Parse(jiraId.Value));
                        break;
                    }
                case "repeaterIssuePriority":
                    {
                        dbId = priorityStorage.GetPriorityMapping(CurrentProjectId, int.Parse(jiraId.Value));
                        break;
                    }
                case "repeaterIssueStatus":
                    {
                        dbId = statusStorage.GetStatusMapping(CurrentProjectId, int.Parse(jiraId.Value));
                        break;
                    }
                case "repeaterIssueResolution":
                    {
                        dbId = resolutionStorage.GetResolutionMapping(CurrentProjectId, int.Parse(jiraId.Value));
                        break;
                    }
                default:
                    break;
            }
            if (dbId > 0)
            {
                dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.OfType<ListItem>().First(i => i.Value == dbId.ToString()));
            }
        }

        protected void dropDownProjects_DataBound(object sender, EventArgs e)
        {
            var projectKey = Request.QueryString["projectKey"];
            if (!string.IsNullOrEmpty(projectKey))
            {
                dropDownProjects.SelectedIndex = dropDownProjects.Items.IndexOf(dropDownProjects.Items.OfType<ListItem>().First(i => i.Text == projectKey));
                dropDownProjects_SelectedIndexChanged(dropDownProjects, EventArgs.Empty);
            }
            if (dropDownProjects.Items.Count == 1)
            {
                dropDownProjects_SelectedIndexChanged(dropDownProjects, EventArgs.Empty);
            }
        }
    }
}